#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time    : 2023/3/4 12:09
# @Author  : WuJunQi
# @Email   : 1479652129@qq.com
# @File    : MyLabel.py
# @Software: PyCharm
from IPython.external.qt_for_kernel import QtCore, QtGui
from PyQt5.QtCore import Qt, QPoint, QRect
from PyQt5.QtGui import QPainter, QPen, QImage, QColor, QPixmap, QPolygon
from PyQt5.QtWidgets import QLabel
from pyqt5_plugins.examplebutton import QtWidgets


class MyLabel(QLabel):
    def __init__(self,parent):
        super().__init__(parent)
        print("mylabel create")

        self.flag = False
        self.isShow = False
        self.point_type = 0  # 0-左键前景点,1-右键背景点
        self.clk_pos = None
        self.x = None
        self.y = None
        self.painter = QPainter(self)
        # 保存四个点的位置以及是否绘画状态以及是否能操作
        self.four_point = []

    def get_four_point(self):
        return self.four_point
    # def eventFilter(self, source, event):
    #     # if the source is our QLabel, it has a valid pixmap, and the event is
    #     # a left click, proceed in trying to get the event position
    #     if (source == self and source.pixmap() and not source.pixmap().isNull() and
    #         event.type() == QtCore.QEvent.MouseButtonPress and
    #         event.button() == QtCore.Qt.LeftButton):
    #             self.getClickedPosition(event.pos())
    #     return super().eventFilter(source, event)

    # def getClickedPosition(self, pos):
    #     # consider the widget contents margins
    #     contentsRect = QtCore.QRectF(self.contentsRect())
    #     if pos not in contentsRect:
    #         # outside widget margins, ignore!
    #         return
    #
    #     # adjust the position to the contents margins
    #     pos -= contentsRect.topLeft()
    #
    #     pixmapRect = self.pixmap().rect()
    #     if self.hasScaledContents():
    #         x = pos.x() * pixmapRect.width() / contentsRect.width()
    #         y = pos.y() * pixmapRect.height() / contentsRect.height()
    #         pos = QtCore.QPoint(x, y)
    #     else:
    #         align = self.alignment()
    #         # for historical reasons, QRect (which is based on integer values),
    #         # returns right() as (left+width-1) and bottom as (top+height-1),
    #         # and so their opposite functions set/moveRight and set/moveBottom
    #         # take that into consideration; using a QRectF can prevent that; see:
    #         # https://doc.qt.io/qt-5/qrect.html#right
    #         # https://doc.qt.io/qt-5/qrect.html#bottom
    #         pixmapRect = QtCore.QRectF(pixmapRect)
    #
    #         # the pixmap is not left aligned, align it correctly
    #         if align & QtCore.Qt.AlignRight:
    #             pixmapRect.moveRight(contentsRect.x() + contentsRect.width())
    #         elif align & QtCore.Qt.AlignHCenter:
    #             pixmapRect.moveLeft(contentsRect.center().x() - pixmapRect.width() / 2)
    #         # the pixmap is not top aligned (note that the default for QLabel is
    #         # Qt.AlignVCenter, the vertical center)
    #         if align & QtCore.Qt.AlignBottom:
    #             pixmapRect.moveBottom(contentsRect.y() + contentsRect.height())
    #         elif align & QtCore.Qt.AlignVCenter:
    #             pixmapRect.moveTop(contentsRect.center().y() - pixmapRect.height() / 2)
    #
    #         if not pos in pixmapRect:
    #             # outside image margins, ignore!
    #             return
    #         # translate coordinates to the image position and convert it back to
    #         # a QPoint, which is integer based
    #         pos = (pos - pixmapRect.topLeft()).toPoint()
    #
    #     print('X={}, Y={}'.format(pos.x(), pos.y()))
    #     return pos.x(),pos.y()

    def mousePressEvent(self, event):
        QLabel.mousePressEvent(self, event)
        # self.clear()
        if event.buttons() == QtCore.Qt.LeftButton:
            print("左键按下")
        self.clk_pos = event.globalPos()
        # self.x = self.getClickedPosition(event.pos())[0]
        # self.y = self.getClickedPosition(event.pos())[1]
        self.x = event.x()
        self.y = event.y()
        if len(self.four_point) == 0:
            self.isShow = True
        if self.isShow:
            self.update()
    def paintEvent(self, event):
        super().paintEvent(event)
        if self.isShow:
            self.four_point.append(QPoint(self.x, self.y))
            self.painter.begin(self)
            self.painter.setPen(QPen(QColor(255, 0, 0), 7))
            self.painter.drawPoint(self.x, self.y)
            self.painter.end()
            print(str(self.x)+" "+str(self.y))
        if self.isShow and len(self.four_point) == 4:
            print("绘画四边形")
            self.painter.begin(self)
            self.painter.setPen(QPen(QColor(255, 0, 0), 6))
            print(self.four_point)
            # 创建四边形的四个点
            self.painter.drawPolygon(QPolygon([
            self.four_point[0],
            self.four_point[1],
            self.four_point[2],
            self.four_point[3],
        ]))
            self.painter.end()
            self.isShow = False

    def mouseReleaseEvent(self, event):
        QLabel.mouseReleaseEvent(self, event)
        # if self.isShow:
        #     self.four_point.append(QPoint(self.x, self.y))



    def getPointGlobalPos(self):
        return self.clk_pos

    # def timerEvent(self, event):
    #     if self.isShow and len(self.four_point) == 4:
    #         print("绘画四边形")
    #         self.painter.begin(self)
    #         self.painter.setPen(QPen(QColor(255, 0, 0), 7))
    #         # 创建四边形的四个点
    #         self.painter.drawPolygon(QPolygon([
    #         self.four_point[0],
    #         self.four_point[1],
    #         self.four_point[2],
    #         self.four_point[3],
    #     ]))
    #         self.painter.end()
    #         self.isShow = False






        # self.painter = QPainter(self)
        # # 设置画笔颜色和宽度
        # self.pen_color = QColor(Qt.black)
        # self.pen_width = 2
        # # 保存上一个鼠标位置
    #     self.last_point = QPoint()
    #     self.scaledContents = True
    #     self.x0 = 0
    #     self.y0 = 0
    #     self.x1 = 0
    #     self.y1 = 0
    #     self.pos0 = None
    #     self.pos1 = None
    #     self.flag = 0
    #     self.isShow = True
    #
    # def paintEvent(self, event):
    #     super().paintEvent(event)
    #     rect = QRect(self.x0, self.y0, abs(self.x1 - self.x0), abs(self.y1 - self.y0))
    #     self.painter.setPen(QPen(Qt.red, 2, Qt.SolidLine))
    #     if self.isShow:
    #         self.painter.save()
    #         self.painter.drawLine(QPoint(self.x0, self.y0),QPoint(self.x1, self.y1))
    #         self.painter.restore()
    #
    # def mousePressEvent(self, event):
    #     QLabel.mousePressEvent(self, event)
    #     self.flag +=1
    #     self.pos0 = event.globalPos()
    #     relative_pos = self.mapFromGlobal(event.globalPos())
    #     self.x0 = relative_pos.x()
    #     self.y0 = relative_pos.y()
    #     self.painter.end()
    #
    # def mouseMoveEvent(self, event):
    #     QLabel.mouseMoveEvent(self, event)
    #     if self.flag < 4:
    #         self.pos1 = event.globalPos()
    #         relative_pos = self.mapFromGlobal(event.globalPos())
    #         self.x1 = relative_pos.x()
    #         self.y1 = relative_pos.y()
    #         self.update()
    #
    # # def mouseReleaseEvent(self, event):
    # #     QLabel.mouseReleaseEvent(self, event)
    # #     self.flag = False
    #
    # def getRectGlobalPos(self):
    #     # 返回绝对坐标
    #     poses = [(self.pos0.x(), self.pos0.y())]
    #     poses.append((self.pos1.x(), self.pos1.y()))
    #     return poses